#include "component.h"
#include "actor.h"


namespace kanon
{
    void Physics2DComponent::Update(float dt)
    {
        auto& comp = owner->GetComponent<Transform2DComponent>();
        comp.position += speed * dt;
    }

    SpriteAnimationComponent::SpriteAnimationComponent(const std::shared_ptr<SpriteSheet>& sheet, float fps)
    {
        this->fps = fps;
        this->sheet = sheet;
        this->index = 0;
        float period = 1.0f / fps;
        this->updater = std::make_shared<PeriodicUpdater>(period);
    }

    void SpriteAnimationComponent::Update(float deltaTime)
    {
        uint32_t offset = updater->Update(deltaTime);
        uint32_t sprite_count = sheet->Count();
        index = (index + offset) % sprite_count;
        uint32_t cols = sheet->Cols();
        uint32_t index_row = index / cols;
        uint32_t index_col = index - index_row * cols;
        this->uvs = this->sheet->GetSubUV(index_row, index_col);
    }
}
